

namespace SharpTalk.Core.Services
{
    using System.Text;
    using System.Security.Cryptography;
    using System;

    /// <summary>
    /// 
    /// </summary>
    public static class SecurityServices
    {
        private static readonly byte[] _salt = new byte[] { 0x17, 0x15, 0x91, 0x26, 0xa4, 0xdc, 0xc4, 0xeb, 0x9b, 0x5a, 0x96, 0x4d, 0xd8, 0x6f, 0x0a, 0x34, 0x11, 0xdc, 0x52, 0xde, 0x2d, 0x38, 0x0a, 0x6e, 0x12, 0xa8, 0x07, 0xc6, 0x1b, 0x87, 0x64, 0x60, 0x87, 0x0b, 0xbd, 0x47, 0xe5, 0xc1, 0x58, 0xd7, 0x02, 0x14, 0x03, 0x22, 0x51, 0xad, 0x56, 0x0a, 0x4f, 0xb5, 0x29, 0x70, 0xf6, 0xe3, 0xf7, 0x8d, 0x49, 0x8e, 0x98, 0x40, 0x6f, 0x13, 0x8e, 0x13, 0xd4, 0xd7, 0x67, 0x3d, 0x14, 0xef, 0x8a, 0x0c, 0xde, 0x8d, 0xfe, 0x1b, 0x8c, 0xcf, 0x5c, 0xab, 0x2e, 0xe7, 0x9a, 0x03, 0xdd, 0x96, 0x72, 0x85, 0x67, 0xd5, 0x98, 0x73, 0x59, 0x9f, 0x50, 0x44, 0x92, 0x39, 0x88, 0x6c, 0x5e, 0xfb, 0x5e, 0xe5, 0x8d, 0x4e, 0x4d, 0xb8, 0x17, 0xed, 0x4d, 0xe8, 0x70, 0x59, 0x7f, 0xe6, 0x8d, 0x46, 0x53, 0x30, 0x2c, 0x7f, 0xf1, 0x23, 0xed, 0x70, 0x3b, 0xf9 };

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] GetHashValue(string data)
        {
            if (string.IsNullOrEmpty(data)) return null;
            var csp = SHA1.Create();
            var result = csp.ComputeHash(Encoding.UTF8.GetBytes(data));
            csp.Clear();

            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] Encrypt(byte[] data)
        {
            return ProtectedData.Protect(data, _salt, DataProtectionScope.CurrentUser);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string EncryptToBase64String(string data)
        {
            return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(data)));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] Decrypt(byte[] data)
        {
            return data.Length > 0
                ? ProtectedData.Unprotect(data, _salt, DataProtectionScope.CurrentUser)
                : data;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string DecryptFromBase64String(string data)
        {
            return String.IsNullOrWhiteSpace(data)
                       ? data
                       : Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(data)));
        }
    }
}